package com.android.lyricshow.ui;

import java.util.ArrayList;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.ViewFlipper;
import android.widget.AdapterView.OnItemClickListener;

import com.android.lyricshow.R;
import com.android.lyricshow.adapter.AlbumSongAdapter;
import com.android.lyricshow.bean.AlbumBean;
import com.android.lyricshow.bean.SongBean;
import com.android.lyricshow.until.AppUntil;
import com.android.lyricshow.until.KEY;

public class BaseAlbumFragment extends BaseTitleBarFragment {
	
	protected View sAlbumDetail			= null;
	
	protected ImageButton sAlbumBg		= null;
	
	protected TextView sAlbumTitle		= null;
	
	protected TextView sAlbumArtist		= null;
	
	protected TextView sAlbumInfo		= null;
	
	protected ListView sSongListView	= null;
	
	protected ViewFlipper sFlipper		= null;
	
	protected Bitmap	sAlbumImage		= null;
	
	protected ArrayList<SongBean> sCurSonglist= null;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		sCurSonglist = new ArrayList<SongBean>();
	}
	
	@Override    
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		
		super.onCreateView(inflater, container, savedInstanceState);
		
		// flipper
		sFlipper	= (ViewFlipper)sRootView.findViewById(R.id.details);		
		sAlbumDetail= (View)sRootView.findViewById(R.id.album_detail);
		
		sSongListView= (ListView)sAlbumDetail.findViewById(R.id.album_song_list);	
		sAlbumBg 	= (ImageButton)sAlbumDetail.findViewById(R.id.ablum_view_bg);	
		sAlbumTitle	= (TextView)sAlbumDetail.findViewById(R.id.album_view_title);
		sAlbumArtist= (TextView)sAlbumDetail.findViewById(R.id.album_view_artist);
		sAlbumInfo	= (TextView)sAlbumDetail.findViewById(R.id.album_view_info);
		
		sSongListView.setOnItemClickListener(mSongListListener);
		return sRootView;
	}
	
	protected void releaseBitmap(){
		// Release bitmap
		if(sAlbumImage != null){
			sAlbumImage.recycle();	
			sAlbumImage = null;
		}
		System.gc();
	}
	
	protected long getTimeByAlbumID(long id, ArrayList<SongBean> mSonglist){
		
		sCurSonglist.clear();
		
		long time = 0;
		for(SongBean sbean : mSonglist){
			if(sbean.getAlbumID() == id){
				sCurSonglist.add(sbean);
				
				time += sbean.getTime();
			}
		}
		
		return AppUntil.toMinute(time);
	}
	
	private OnItemClickListener mSongListListener = new OnItemClickListener(){

		@Override
		public void onItemClick(AdapterView<?> adapter, View view, int index,
				long id) {
			
			if(index > sCurSonglist.size() - 1){
				AppUntil.showToast(sContext, "The index is out of song list!");
				return;
			}
			
			SongBean sbean = (SongBean)adapter.getItemAtPosition(index);
			
			AppUntil.showToast(sContext, "You select " + sbean.getTitle());
			
			Intent it = new Intent(sContext, PlayActivity.class);
			it.putExtra(KEY.SONG_BEAN_LIST, sCurSonglist);
			it.putExtra(KEY.CURRENT_INDEX, index);
			
			goToPlay(it);
		}		
	};
	
	protected void goToPlay(Intent it){
		startActivity(it);
		getActivity().overridePendingTransition(R.anim.push_right_in, R.anim.push_left_out);
	}
	
	protected void updateAlbumDetail(AlbumBean abean, ArrayList<SongBean> mSonglist){
		
		if(abean == null){
			return;
		}
		
		releaseBitmap();
		
		sAlbumImage = AppUntil.getBitmapFromDB(sContext, abean.getID(), AppUntil.SIZE_NORMAL);
		if(sAlbumImage != null){
			sAlbumBg.setImageBitmap(sAlbumImage);
		}else{
			sAlbumBg.setImageResource(R.drawable.noalbum);
		}
		
		long min = getTimeByAlbumID(abean.getID(), mSonglist);
		
		String info = sContext.getResources().getString(R.string.num_of_song);
		
		sAlbumTitle.setText(abean.getName());
		sAlbumArtist.setText(abean.getArtist());
		sAlbumInfo.setText(String.format(info, sCurSonglist.size() + "", "" + min));
		
		sSongListView.setAdapter(new AlbumSongAdapter(sContext, sCurSonglist));
	}
}
